package com.lxmes.sales.service.impl;

import java.util.List;

import com.lxmes.common.core.domain.entity.SysDept;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.AssetCode;
import com.lxmes.sales.mapper.AssetCodeMapper;
import com.lxmes.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.AssetfilesMapper;
import com.lxmes.sales.domain.Assetfiles;
import com.lxmes.sales.service.IAssetfilesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产档案Service业务层处理
 *
 * @author cdc
 * @date 2024-08-14
 */
@Service
public class AssetfilesServiceImpl implements IAssetfilesService
{
    @Autowired
    private AssetfilesMapper assetfilesMapper;
    @Autowired
    private AssetCodeMapper assetCodeMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询资产档案
     *
     * @param id 资产档案主键
     * @return 资产档案
     */
    @Override
    public Assetfiles selectAssetfilesById(Long id)
    {
        return assetfilesMapper.selectAssetfilesById(id);
    }

    /**
     * 查询资产档案列表
     *
     * @param assetfiles 资产档案
     * @return 资产档案
     */
    @Override
    public List<Assetfiles> selectAssetfilesList(Assetfiles assetfiles)
    {
        return assetfilesMapper.selectAssetfilesList(assetfiles);
    }

    /**
     * 新增资产档案
     *
     * @param assetfiles 资产档案
     * @return 结果
     */
    @Override
    public int insertAssetfiles(Assetfiles assetfiles)
    {
        assetfiles.setCreateBy(SecurityUtils.getUsername());
        assetfiles.setCreateTime(DateUtils.getNowDate());
        return assetfilesMapper.insertAssetfiles(assetfiles);
    }

    /**
     * 修改资产档案
     *
     * @param assetfiles 资产档案
     * @return 结果
     */
    @Override
    public int updateAssetfiles(Assetfiles assetfiles)
    {
        assetfiles.setFillingBy(SecurityUtils.getUsername());
        assetfiles.setFillingTime(DateUtils.getNowDate());
        return assetfilesMapper.updateAssetfiles(assetfiles);
    }

    /**
     * 批量删除资产档案
     *
     * @param ids 需要删除的资产档案主键
     * @return 结果
     */
    @Override
    public int deleteAssetfilesByIds(Long[] ids)
    {
        return assetfilesMapper.deleteAssetfilesByIds(ids);
    }

    /**
     * 删除资产档案信息
     *
     * @param id 资产档案主键
     * @return 结果
     */
    @Override
    public int deleteAssetfilesById(Long id)
    {
        return assetfilesMapper.deleteAssetfilesById(id);
    }
    @Override
    @Transactional
    public String imporIteminfo(List<Assetfiles> assetfilesList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(assetfilesList) || assetfilesList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Assetfiles assetfiles : assetfilesList)
        {
            try
            {
                BeanValidators.validateWithException(validator, assetfiles);
                assetfiles.setCreateBy(operName);
                assetfiles.setUpdateBy(operName);

                int ts=Integer.parseInt(assetfiles.getUseLife());
                for (int i = 0; i < ts; i++) {
                    AssetCode assetCodeQuery=new AssetCode();
                    assetCodeQuery.setCategoryName(assetfiles.getAssetType());
                    assetCodeQuery.setSubcategoryName(assetfiles.getAssetClass());
                    assetCodeQuery.setAssetName(assetfiles.getAssetName());
                    AssetCode assetCode= assetCodeMapper.selectAssetCodeByObj(assetCodeQuery);
                    if (assetCode == null){
                        throw new ServiceException("资产编码【分类名称:"+assetfiles.getAssetType()+"子类名称:"+assetfiles.getAssetClass()+"资产名称:"+assetfiles.getAssetName()+"】不存在");
                    }
                    assetfiles.setAssetCode(assetCode.getAssetCode());
                    assetfiles.setAssetNumber(assetCode.getAssetNumber());
                    SysDept dept = deptMapper.selectDeptByName(assetfiles.getSapProof());
                    if(dept!=null){
                        assetfiles.setReceivingUnit(dept.getDeptId());
                    }
                    String newNumber=assetfiles.getAssetCode()+ NewNumber.createNewNumber3(assetfilesMapper.get3NewCode(assetfiles.getAssetCode()));
                    assetfiles.setBarcode(newNumber);
                    this.insertAssetfiles(assetfiles);
                }
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
